



package jnpf.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.config.ConfigValueUtil;
import jnpf.entity.DocumentEntity;
import jnpf.entity.MzCompilationprojectEntity;
import jnpf.entity.MzCompilationtypeEntity;
import jnpf.entity.MzProjectcontentEntity;
import jnpf.exception.DataException;
import jnpf.model.mzprojectcontent.*;
import jnpf.permission.entity.UserEntity;
import jnpf.service.DocumentService;
import jnpf.service.MzCompilationprojectService;
import jnpf.service.MzCompilationtypeService;
import jnpf.service.MzProjectcontentService;
import jnpf.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * 采编内容
 * mzProjectcontent
 * @版本： V3.2.0
 * @版权： 引迈信息技术有限公司（https://www.jnpfsoft.com）
 * @作者： JNPF开发平台组
 * @日期： 2023-12-10
 */
@Slf4j
@RestController
@Tag(name = "mzProjectcontent" , description = "example")
@RequestMapping("/api/example/MzProjectcontent")
public class MzProjectcontentController {

    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private ConvertUtils convertUtils;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private ConfigValueUtil configValueUtil;

    @Autowired
    private MzProjectcontentService mzProjectcontentService;

    @Autowired
    private DocumentService documentService;

    @Autowired
    private MzCompilationprojectService mzCompilationprojectService;

    @Autowired
    private MzCompilationtypeService mzCompilationtypeService;

    /**
     * 列表
     *
     * @param mzProjectcontentPagination
     * @return
     */
    @Operation(summary = "获取列表")
	@PostMapping("/getList")
    public ActionResult list(@RequestBody MzProjectcontentPagination mzProjectcontentPagination)throws IOException{
    //树形判断是否有参数，有参数不造树
        if(StringUtil.isEmpty(mzProjectcontentPagination.getCompilationProjectId())){
            return ActionResult.fail("采编项目主键为空！");
        }
        List<MzProjectcontentEntity> list= mzProjectcontentService.getList(mzProjectcontentPagination);
        //处理id字段转名称，若无需转或者为空可删除

    List<MzProjectcontentListVO> listVO=JsonUtil.getJsonToList(list,MzProjectcontentListVO.class);
    for(MzProjectcontentListVO mzProjectcontentVO:listVO){
        ActionResult info = info(mzProjectcontentVO.getId());
        Map<String, Object> dataMap = JsonUtil.entityToMap(info.getData());

                mzProjectcontentVO.setCompilationtypeid_id(mzProjectcontentVO.getCompilationtypeid_name());

                MzCompilationtypeEntity entity = mzCompilationtypeService.getInfo(mzProjectcontentVO.getCompilationtypeid());
                if(StringUtil.isNotNull(entity)){
                    mzProjectcontentVO.setCompilationtypeid_name(entity.getTypename());
                    if(StringUtil.isNotEmpty(entity.getParentId())){
                        MzCompilationtypeEntity parent = mzCompilationtypeService.getInfo(entity.getParentId());
                        mzProjectcontentVO.setCompilationtypeid_parentname(parent.getTypename());
                    }else{
                        mzProjectcontentVO.setCompilationtypeid_parentname(entity.getTypename());
                    }
                }else{
                    mzProjectcontentVO.setCompilationtypeid_name(mzProjectcontentVO.getCompilationtypeid());
                }

                mzProjectcontentVO.setTitle_name(mzProjectcontentVO.getTitle());

                mzProjectcontentVO.setSort_name(generaterSwapUtil.getDecimalStr(mzProjectcontentVO.getSort()));
                if(StringUtil.isNotEmpty(mzProjectcontentVO.getContentfile())){
                    DocumentEntity doc = documentService.getInfo(mzProjectcontentVO.getContentfile());
                    mzProjectcontentVO.setContentfile_name(convertUtils.DocumentConvertUrl(doc.getFileExtension(),doc.getFilePath()));
                }
                if(StringUtil.isNotEmpty(mzProjectcontentVO.getCover())){
                    DocumentEntity doc = documentService.getInfo(mzProjectcontentVO.getCover());
                    mzProjectcontentVO.setCover_name(convertUtils.DocumentConvertUrl(doc.getFileExtension(),doc.getFilePath()));

                }

            mzProjectcontentVO.setKeyword_name(generaterSwapUtil.getDicName(mzProjectcontentVO.getKeyword(),"503138438824853509"));

                mzProjectcontentVO.setIllustrate_name(mzProjectcontentVO.getIllustrate());

                mzProjectcontentVO.setEditcontent_name(mzProjectcontentVO.getEditcontent());

                MzCompilationprojectEntity  mzCompilationprojectEntity = mzProjectcontentService.getMzCompilationproject(mzProjectcontentVO.getCompilationprojectid());
	if(ObjectUtil.isNotEmpty(mzCompilationprojectEntity)){
            MzCompilationprojectListVO  mzCompilationprojectListVO = JsonUtil.getJsonToBean(mzCompilationprojectEntity,MzCompilationprojectListVO.class);
                mzCompilationprojectListVO.setProjectname_name(mzCompilationprojectListVO.getProjectname());

            mzProjectcontentVO.setMz_compilationproject(mzCompilationprojectListVO);
			}
		//子表数据转换
     }

         PageListVO vo=new PageListVO();
         vo.setList(listVO);
         PaginationVO page=JsonUtil.getJsonToBean(mzProjectcontentPagination,PaginationVO.class);
         vo.setPagination(page);
         return ActionResult.success(vo);

    }

    /**
     * 创建
     *
     * @param mzProjectcontentForm
     * @return
     */
    @PostMapping
    @Transactional
    @Operation(summary = "创建")
    public ActionResult create(@RequestBody @Valid MzProjectcontentForm mzProjectcontentForm) throws DataException {
        String b = mzProjectcontentService.checkForm(mzProjectcontentForm,0);
        if (StringUtil.isNotEmpty(b)){
         return ActionResult.fail(b + "不能重复");
        }
        String mainId =RandomUtil.uuId();

        UserInfo userInfo=userProvider.get();
		UserEntity userEntity = generaterSwapUtil.getUser(userInfo.getUserId());
        GeneraterSwapUtil.swapDatetime(mzProjectcontentForm);
        MzProjectcontentEntity entity = JsonUtil.getJsonToBean(mzProjectcontentForm, MzProjectcontentEntity.class);
            entity.setId(mainId);
                mzProjectcontentService.save(entity);

        return ActionResult.success("创建成功");
}


    /**
    * 信息
    *
    * @param id
    * @return
    */
    @Operation(summary = "信息")
    @GetMapping("/{id}")
    public ActionResult<MzProjectcontentInfoVO> info(@PathVariable("id") String id){
        MzProjectcontentEntity entity= mzProjectcontentService.getInfo(id);
        MzProjectcontentInfoVO vo=JsonUtil.getJsonToBean(entity, MzProjectcontentInfoVO.class);

    //子表
    //副表
		QueryWrapper<MzCompilationprojectEntity> queryWrapperMzCompilationproject = new QueryWrapper<>();
        queryWrapperMzCompilationproject.lambda().eq(MzCompilationprojectEntity::getId,entity.getCompilationprojectid());
        MzCompilationprojectEntity mz_compilationprojectEntity = mzCompilationprojectService.getOne(queryWrapperMzCompilationproject);
        MzCompilationprojectModel mzCompilationprojectModel = JsonUtil.getJsonToBean(mz_compilationprojectEntity, MzCompilationprojectModel.class);
		vo.setMz_compilationproject(mzCompilationprojectModel);
        if(StringUtil.isNotEmpty(vo.getContentfile())){
            DocumentEntity doc = documentService.getInfo(vo.getContentfile());
            vo.setContentfilename(doc.getFullName());
        }
        if(StringUtil.isNotEmpty(vo.getCover())){
            DocumentEntity doc = documentService.getInfo(vo.getCover());
            vo.setCovername(doc.getFullName());
        }
        return ActionResult.success(vo);
    }

    /**
    * 表单信息(详情页)
    *
    * @param id
    * @return
    */
    @Operation(summary = "表单信息(详情页)")
    @GetMapping("/detail/{id}")
    public ActionResult<MzProjectcontentInfoVO> detailInfo(@PathVariable("id") String id){
        ActionResult info = info(id);
        Map<String,Object> dataMap = JsonUtil.entityToMap(info.getData());
        MzProjectcontentEntity entity= mzProjectcontentService.getInfo(id);
        MzProjectcontentInfoVO vo=JsonUtil.getJsonToBean(entity, MzProjectcontentInfoVO.class);

 //子表数据转换

 //附表数据转换
        QueryWrapper<MzCompilationprojectEntity> queryWrapperMzCompilationproject = new QueryWrapper<>();
            queryWrapperMzCompilationproject.lambda().eq(MzCompilationprojectEntity::getId,entity.getCompilationprojectid());
        MzCompilationprojectEntity mz_compilationprojectEntity = mzCompilationprojectService.getOne(queryWrapperMzCompilationproject);
		if(ObjectUtil.isEmpty(mz_compilationprojectEntity)){
            mz_compilationprojectEntity = new MzCompilationprojectEntity();
		}

            MzCompilationprojectModel mzCompilationprojectModel = JsonUtil.getJsonToBean(mz_compilationprojectEntity, MzCompilationprojectModel.class);
                mzCompilationprojectModel.setProjectname(mzCompilationprojectModel.getProjectname());


		    vo.setMz_compilationproject(mzCompilationprojectModel);

//添加到详情表单对象中

                vo.setCompilationtypeid_id(vo.getCompilationtypeid());

                MzCompilationtypeEntity compilationtypeEntity = mzCompilationtypeService.getInfo(vo.getCompilationtypeid());
                if(StringUtil.isNotNull(compilationtypeEntity)){
                    vo.setCompilationtypeid_id(compilationtypeEntity.getTypename());
                }else{
                    vo.setCompilationtypeid_id(vo.getCompilationtypeid());
                }
                vo.setTitle(vo.getTitle());

                vo.setSort_name(generaterSwapUtil.getDecimalStr(vo.getSort()));

                if(StringUtil.isNotEmpty(vo.getContentfile())){
                    DocumentEntity doc = documentService.getInfo(vo.getContentfile());
                    vo.setContentfile(doc.getFullName());
                }
                if(StringUtil.isNotEmpty(vo.getCover())){
                    DocumentEntity doc = documentService.getInfo(vo.getCover());
                    vo.setCover(doc.getFullName());
                }

            vo.setKeyword(generaterSwapUtil.getDicName(vo.getKeyword(),"503138438824853509"));

                vo.setIllustrate(vo.getIllustrate());

                vo.setEditcontent(vo.getEditcontent());


        return ActionResult.success(vo);
		}




   /**
    * 更新
    *
    * @param id
    * @param mzProjectcontentForm
    * @return
    */
    @PutMapping("/{id}")
    @Transactional
    @Operation(summary = "更新")
    public ActionResult update(@PathVariable("id") String id,@RequestBody @Valid MzProjectcontentForm mzProjectcontentForm) throws DataException {
        String b =  mzProjectcontentService.checkForm(mzProjectcontentForm,1);
        if (StringUtil.isNotEmpty(b)){
            return ActionResult.fail(b + "不能重复");
        }
        UserInfo userInfo=userProvider.get();
        MzProjectcontentEntity entity= mzProjectcontentService.getInfo(id);
        if(entity!=null){
            GeneraterSwapUtil.swapDatetime(mzProjectcontentForm);
            MzProjectcontentEntity subentity=JsonUtil.getJsonToBean(mzProjectcontentForm, MzProjectcontentEntity.class);
				boolean b1 = mzProjectcontentService.updateById(subentity);
				if (!b1){
				return ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据");
				}
						//子表数据
/*				if(ObjectUtil.isNotEmpty(mzProjectcontentForm.getMz_compilationproject())){
                    QueryWrapper<MzCompilationprojectEntity> queryWrapperMzCompilationproject =new QueryWrapper<>();
                    queryWrapperMzCompilationproject.lambda().eq(MzCompilationprojectEntity::getId,entity.getCompilationprojectid());
                MzCompilationprojectEntity mz_compilationprojectOneEntity= mzCompilationprojectService.getOne(queryWrapperMzCompilationproject);
                MzCompilationprojectEntity  mz_compilationprojectentity=JsonUtil.getJsonToBean(mzProjectcontentForm.getMz_compilationproject(), MzCompilationprojectEntity.class);
                mz_compilationprojectentity.setId(mz_compilationprojectOneEntity.getId());
                    mz_compilationprojectentity.setId(entity.getCompilationprojectid());
                mzCompilationprojectService.updateById(mz_compilationprojectentity);
				}*/
        return ActionResult.success("更新成功");
        }else{
            return ActionResult.fail("更新失败，数据不存在");
        }
    }



   /**
    * 删除
    *
    * @param id
    * @return
    */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Transactional
    public ActionResult delete(@PathVariable("id") String id){
        MzProjectcontentEntity entity= mzProjectcontentService.getInfo(id);
        if(entity!=null){
            mzProjectcontentService.delete(entity);
						//子表数据
				QueryWrapper<MzCompilationprojectEntity> queryWrapperMzCompilationproject=new QueryWrapper<>();
                    queryWrapperMzCompilationproject.lambda().eq(MzCompilationprojectEntity::getId,entity.getCompilationprojectid());
                mzCompilationprojectService.remove(queryWrapperMzCompilationproject);
        }
        return ActionResult.success("删除成功");
    }






}
